package com.woniuxy.happylife.controller;

import com.woniuxy.happylife.entity.RbacMenu;
import com.woniuxy.happylife.entity.RbacRole;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.service.RbacMenuService;
import com.woniuxy.happylife.service.RbacRoleService;
import com.woniuxy.happylife.util.JwtUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Classname RoleController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/2/17 14:53
 * @Created by hang
 */
@RestController
@RequestMapping("/rolepermmenu/menu")
public class MenuController {

    @Resource
    RbacMenuService rbacMenuService;
    @GetMapping("/findByToken")
    public ResponseEntity<List<RbacMenu>> findByToken( HttpServletRequest request){
        //获取请求头，根据请求头token得到username
        String token = request.getHeader("Authorization");
        System.out.println(token);

        String username = JwtUtil.getUname(token);
        List<RbacMenu> rbacMenus = rbacMenuService.findByUsername(username);
        if(rbacMenus==null){
            return new ResponseEntity<List<RbacMenu>>(401, "获取菜单失败", null);
        }else{
            return new ResponseEntity<List<RbacMenu>>(200, "查询成功", rbacMenus);
        }

    }

    @GetMapping("/findByRoleId/{roleId}")
    public ResponseEntity findByRoleId(@PathVariable("roleId")int roleId){
        List <String> menuByRoleId = rbacMenuService.findMenuByRoleId(roleId);
        if(menuByRoleId==null){
            return new ResponseEntity<>(401, "获取菜单失败", null);
        }else{
            return new ResponseEntity<>(200, "查询成功", menuByRoleId);
        }
    }

    @PutMapping("/updateMenus")
    public ResponseEntity delMenusByRoleId(@RequestBody List<String> array){
//        System.out.println(Array);
        int i = rbacMenuService.updateMenus(array);
        if(i>0){
            return new ResponseEntity(200,"修改成功",null);
        }else {
            return new ResponseEntity(401,"修改失败",null);

        }

    }

}
